package com.newtouch.bxzs.database.synchronize.batch.writer;

import com.newtouch.bxzs.database.synchronize.mapper.CustomerMapper;
import com.newtouch.bxzs.database.synchronize.model.CustomerBO;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 客户同步入库
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/7 10:50</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author yuanjie
 * @version 1.0.0
 * @since 1.0.0
 */
@Component
@SuppressWarnings("unchecked")
public class WriterCustomer {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 同步客户数据至数据库
     * @return ItemWriter
     */
    public ItemWriter<CustomerBO> addCustomerList(){
        return list -> {
            customerMapper.addCustomer((List<CustomerBO>) list,new Date());
            //获取本地没有的客户
            List<CustomerBO> addCustomer = customerMapper.getAddCustomer();
            //获取需要更新客户标识的数据
            List<CustomerBO> getCustomerTag = customerMapper.getcustomerLabel();
            //获取可更新的客户信息
            List<CustomerBO> getUpdateCustomer = customerMapper.getUpdateCustomer();
            if (!addCustomer.isEmpty()){
                customerMapper.addCustomerList(addCustomer,new Date());
            }
            if (getCustomerTag.size() > 0){
                customerMapper.updatecustLabel(getCustomerTag,new Date());
            }
            if (getUpdateCustomer.size() > 0){
                customerMapper.updateCustomerBatch(getUpdateCustomer,new Date());
            }
        };
    }
}
